package com.itheima.controller;

import com.itheima.MessageConstant;
import com.itheima.pojo.Blog;
import com.itheima.service.BlogService;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客归档相关接口
 */
@RestController
@RequestMapping("/sort")
@Api(tags = "博客归档相关接口")
public class SortController {
    @Autowired
    BlogService blogService;

    //通过月份获取文章信息
    @ApiOperation("通过月份获取文章")
    @GetMapping("/getArticleByMonth/{monthDate}")
    public String getArticleByMonth(@PathVariable String monthDate){
        if (monthDate==null||"".equals(monthDate)){
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST,"请输入月份");
        }
        try {
            List<Blog> blogList=blogService.getArticleByMonth(monthDate);
            return ResultUtil.result(HttpServletResponse.SC_OK,blogList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.GET_ARTICLE_BY_MONTH_FAIL);
        }
    }

    //根据月份归档
    @GetMapping("/getSortList")
    @ApiOperation("归档")
    public String getSortList(){
        try {
            List<String> monthList=blogService.getSortList();
            return ResultUtil.result(HttpServletResponse.SC_OK,monthList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.GET_ARTICLE_BY_MONTH_FAIL);
        }
    }
}
